﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.DAL;
using System.Data.SqlClient;
using SeDuce.BLL;
using SeDuce.Model;
using System.IO;

public partial class Management_Products_PicList : RepeaterPagerBase
{
    List<int> picIDs = new List<int>();
    Lazy_Yu<PicBLL> lazyPicBLL = new Lazy_Yu<PicBLL>(() => new PicBLL());
    public PicBLL LazyPicBLL
    {
        get { return this.lazyPicBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlParameter[] sp = new SqlParameter[] { 
            new SqlParameter ("@ProductID", this.GetQueryString("ProductID", false).ToInt())
        };
        this.RegistrPager(this.Repeater1, this.rptPageFliper, 7, this.FillPageMoudleWithParams(50, string.Format(SeDuce.DAL.PicDAL.sqlPicsByProductID, "@ProductID"), sp, new List<SortField>() { 
            new SortField() { FieldName = "Sort", DESC = false }
        }));
    }
    protected void lbtnADD_Click(object sender, EventArgs e)
    {
        Response.Redirect("PicUpload.aspx?ProductID=" + this.GetQueryString("ProductID", false));
    }
    //protected void lbtnSort_Click(object sender, EventArgs e)
    //{
    //    List<Pic> pics = new List<Pic>();
    //    for (int i=0;i<this.Repeater1.Items.Count;i++)
    //    {
    //        RepeaterItem item = this.Repeater1.Items[i];
    //        HiddenField hidPicID = (HiddenField)item.FindControl("hidPicID");
    //        HyperLink hlnkUrl = (HyperLink)item.FindControl("hlnkUrl");
    //        Label lblName = (Label)item.FindControl("lblName");
    //        pics.Add(new Pic()
    //        {
    //            PicID = hidPicID.Value.ToInt(),
    //            PicName = lblName.Text,
    //            ProductID = this.GetQueryString("ProductID", false).ToInt(),
    //            Url = hlnkUrl.ToolTip,
    //            Sort = i
    //        });
    //    }
    //    if (this.LazyPicBLL.UpdatePics(pics))
    //    {
    //        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存成功!"), true);
    //    }
    //    else
    //    {
    //        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
    //    }

    //}
    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        List<string> file_list_to_delete = new List<string>();

        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chkCheck");
            if (chk.Checked)
            {
                HiddenField hidPicID = (HiddenField)item.FindControl("hidPicID");
                HyperLink hlnkUrl = (HyperLink)item.FindControl("hlnkUrl");
                file_list_to_delete.Add(hlnkUrl.ToolTip);
                picIDs.Add(hidPicID.Value.ToInt());
            }
        }
        if (this.LazyPicBLL.DeletePics(picIDs))
        {
            foreach (string file in file_list_to_delete)
            {
                if (File.Exists(Server.MapPath(file)))
                {
                    File.Delete(Server.MapPath(file));
                }
            }
        }
        this.BindPage(this.rptPageFliper.UniqueID, 1);
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:resetsortable();", true);
    }
    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("List.aspx");
    }
}